<?php
    /*
        使用方法：先用命令行开启该服务： php swoole.php
        页面使用方法：参考public下的chat.html
    */
 $server = new swoole_websocket_server("0.0.0.0", 9501);

 $server->on('open', function (swoole_websocket_server $server, $request) {
    $fd = $request->fd;
    var_dump($request->server['path_info']);
    echo "server: handshake success with fd{$fd}\n";
    $server->push( $fd, '您的fd是：'.$fd);
 });

 $server->on('message', function (swoole_websocket_server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
     foreach ( $server->connections as $fd )
     {
        $server->push( $fd, $frame->data );
     }
 });

 $server->on('close', function ($ser, $fd) {

    echo "client {$fd} closed\n";

 });
 $server->on('start', function () {

    echo "client start\n";

 });

 $server->start();